package h0913;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res=new ArrayList<>();
        boolean[] used=new boolean[nums.length];
        backTrack(res,new LinkedList<>(),used,nums);
        return res;
    }

    private void backTrack(List<List<Integer>> res, LinkedList<Integer> temp, boolean[] used,int[] nums) {
        if (temp.size()== nums.length){
            res.add(new LinkedList<>(temp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!used[i]){
                temp.addLast(nums[i]);
                used[i]=true;
                backTrack(res, temp, used, nums);
                temp.removeLast();
                used[i]=false;
            }
        }
    }

    public static void main(String[] args) {
        int[] nums={1,2,3};
        Solution solution = new Solution();
        System.out.println(solution.permute(nums));
    }
}
